package com.since.data_structure.stack;

/**
 * Created by Sincerity on 2020/5/9.
 * 描述：
 */
public class ArrayStack {
    private int maxSize;
    private int index = -1;
    private int[] array;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        array = new int[maxSize];
    }
    public int pop() {
        if (isEmpty()) throw new RuntimeException("站内元素为空,出栈失败");
        int value = array[index];
        index--;
        return value;
    }

    public void show() {
        if (isEmpty()) {
            System.out.println("空栈");
            return;
        }
        for (int i = index; i >= 0; i--) {
            System.out.printf("array[%d]=%d\n", i, array[i]);
        }
    }

    public void push(int value) {
        if (isFull()) {
            System.out.println("沾满");
            return;
        }
        index++;
        array[index] = value;
    }

    public boolean isFull() {
        return maxSize - 1 == index;
    }

    public boolean isEmpty() {
        return index == -1;
    }

    public int peek() {
        return array[index];
    }
}
